# -*- coding: utf-8 -*-
VocLst = ['pA1', 'pA2', 'pA3',
          'pB1', 'pB2', 'pB3',
          'pC1', 'pC2', 'pC3',
          'oA1', 'oA2', 'oA3',
          'oB1', 'oB2', 'oB3',
          'oC1', 'oC2', 'oC3',
          'NA']
stepLst = ['A1', 'A2', 'A3',
           'B1', 'B2', 'B3',
           'C1', 'C2', 'C3',
           'NA']
Board = dict(A1='A1', A2='A2', A3='A3', B1='B1', B2='B2', B3='B3', C1='C1', C2='C2', C3='C3')


def newGame():
    return dict(A1=None, A2=None, A3=None, B1=None, B2=None, B3=None, C1=None, C2=None, C3=None)


def game2gameData(game):
    return [
        [game['A1'], game['A2'], game['A3']],
        [game['B1'], game['B2'], game['B3']],
        [game['C1'], game['C2'], game['C3']]
    ]


def checkStepIlligal(step, availableSteps):
    while step not in stepLst[:-1]:
        step = raw_input('please entry[A1-C3]:').upper()
    while step not in availableSteps:
        step = raw_input('position is occopied!\nplease select new one[A1-C3]:').upper()
    return step


def isGameOver(game):
    x = len(game)
    y = len(game[0])
    for i in range(x):
        if len(set(game[i])) == 1 and game[i][0] is not None:
            return 1, game[i][0]
    for i in range(y):
        arr = []
        for j in range(x):
            arr.append(game[j][i])
        if len(set(arr)) == 1 and arr[0] is not None:
            return 1, arr[0]
    i = 0
    j = 0
    arr = []
    while i < x and j < y:
        arr.append(game[i][j])
        i += 1
        j += 1
    if len(set(arr)) == 1 and arr[0] is not None:
        return 1, arr[0]
    i = x - 1
    j = 0
    arr = []
    while i >= 0 and j < y:
        arr.append(game[i][j])
        i -= 1
        j += 1
    if len(set(arr)) == 1 and arr[0] is not None:
        return 1, arr[0]
    extendGame = []
    for i in range(x):
        extendGame.extend(game[i])
    if None not in set(extendGame):
        return 1, 'Draw'
    return 0, None


def fillSteps(steps, offset=0):
    cl = []
    cl.extend(steps or [])
    cl.extend([stepLst[-1]]*(5-offset-len(steps or [])))
    return cl


def trans2SelfSteps(steps):
    sSteps = ['p' + step for step in steps]
    return sSteps


def trans2OppSteps(steps):
    oSteps = ['o' + step for step in steps]
    return oSteps
